#include <iostream>
#include <string>
#include <unordered_map>
#include <fstream>
#include "InetAddr.hpp"
#include "Log.hpp"

using namespace LogModule;
const std::string defaultdictpath = "./dictionary.txt";
const std::string gep = ": ";
class Dict
{
public:
    Dict(const std::string path = defaultdictpath)
        : _dict_path(path)
    {
    }

    bool LoadDict()
    {
        std::ifstream in(_dict_path);

        if (!in.is_open())
        {
            LOG(LogLevel::DEBUG) << "打开字典" << _dict_path << "错误";
            return false;
        }

        std::string line;
        while (std::getline(in, line))
        {
            auto pos = line.find(gep);
            if (pos == std::string::npos)
            {
                LOG(LogLevel::WARNING) << "解析: " << line << " 失败";
                continue;
            }

            std::string english = line.substr(0, pos);
            std::string chinese = line.substr(pos + gep.size());
            if (english.empty() || chinese.empty())
            {
                LOG(LogLevel::WARNING) << "没有有效内容: " << line;
                continue;
            }
            _dict.insert({english, chinese});
            LOG(LogLevel::DEBUG) << "加载" << line;
        }
        in.close();
        return true;
    }

    std::string Translate(const std::string &word, InetAddr &client)
    {
        auto iter = _dict.find(word);
        if (iter == _dict.end())
        {
            LOG(LogLevel::DEBUG) << "进入到了翻译模块, [" << client.Ip() << ":" << client.Port() << "]# " << word << "->None";
            return "None";
        }

        LOG(LogLevel::DEBUG) << "进入到了翻译模块, [" << client.Ip() << ":" << client.Port() << "]# " << word << "->" << _dict[word];
        return _dict[word];
    }
    ~Dict()
    {
    }

private:
    std::string _dict_path; // 路径+文件名
    std::unordered_map<std::string, std::string> _dict;
};